package com.lagou.controller;

import com.lagou.domain.ResourceCategory;
import com.lagou.domain.ResponseResult;
import com.lagou.service.ResourceCategoryService;
import com.lagou.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ResourceCategory")
public class ResourceCategoryController {

    @Autowired
    private ResourceCategoryService resourceCategoryService;

    @Autowired
    private ResourceService resourceService;
    @RequestMapping("/findAllResourceCategory")
    public ResponseResult findAllResourceCategory(){
        List<ResourceCategory> allResourceCategory = resourceCategoryService.findAllResourceCategory();
        return  new ResponseResult(true,200,"查询所有分类信息成功",allResourceCategory);
    }

    /**
     * 保存资源分类
     * @param resourceCategory
     * @return
     */
    @RequestMapping("/saveOrUpdateResourceCategory")
    public ResponseResult saveOrUpdateResourceCategory(@RequestBody ResourceCategory resourceCategory){
        resourceCategoryService.saveOrUpdateResourceCategory(resourceCategory);
        return  new ResponseResult(true,200,"保存成功",null);
    }

    /**
     * 删除资源分类
     * @param id
     * @return
     */
    @RequestMapping("/deleteResourceCategory")
    public ResponseResult deleteResourceCategory(int id){
        if (resourceService.findAllResourceByCategoryId(id).size()>0){
            return  new ResponseResult(true,401,"删除失败，该资源分类下存在资源尚未删除",null);
        }else{
            resourceCategoryService.deleteResourceCategory(id);
            return  new ResponseResult(true,200,"删除成功",null);
        }

    }
}
